package com.dmap.run.service.impl;

import com.dmap.run.bo.background.UserPortraitMovementInfoBo;
import com.dmap.run.dao.ClimbingRecordMapper;
import com.dmap.run.dao.RidingRecordMapper;
import com.dmap.run.dao.RunRecordMapper;
import com.dmap.run.dao.SwimRecordMapper;
import com.dmap.run.po.*;
import com.dmap.run.service.UserPortraitMovementService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author liuying
 * @Description 用户画像Rpc服务层实现
 * @Date 18:27 2018/7/2
 */
@Service
public class UserPortraitMovementServiceImpl implements UserPortraitMovementService {
    @Resource
    private RunRecordMapper runRecordMapper;

    @Resource
    private RidingRecordMapper ridingRecordMapper;

    @Resource
    private ClimbingRecordMapper climbingRecordMapper;

    @Resource
    private SwimRecordMapper swimRecordMapper;

    @Override
    public UserPortraitMovementInfoBo selectUserMovementInfo(Long userId) {
        //跑步数据查询
        RunRecordExample runRecordExample = new RunRecordExample();
        runRecordExample.createCriteria().andUserIdEqualTo(userId);
        List<RunRecord> runRecords = runRecordMapper.selectByExample(runRecordExample);

        //骑行数据查询
        RidingRecordExample ridingRecordExample = new RidingRecordExample();
        ridingRecordExample.createCriteria().andUserIdEqualTo(userId);
        List<RidingRecord> ridingRecords = ridingRecordMapper.selectByExample(ridingRecordExample);

        //登山数据查询
        ClimbingRecordExample climbingRecordExample = new ClimbingRecordExample();
        climbingRecordExample.createCriteria().andUserIdEqualTo(userId);
        List<ClimbingRecord> climbingRecords = climbingRecordMapper.selectByExample(climbingRecordExample);

        //游泳数据查询
        SwimRecordExample swimRecordExample = new SwimRecordExample();
        swimRecordExample.createCriteria().andUserIdEqualTo(userId);
        List<SwimRecord> swimRecords = swimRecordMapper.selectByExample(swimRecordExample);

        //汇总数据
        UserPortraitMovementInfoBo userPortraitMovementInfoBo = new UserPortraitMovementInfoBo();
        if (CollectionUtils.isEmpty(runRecords)){
            userPortraitMovementInfoBo.setRunRecord(null);
        } else {
            userPortraitMovementInfoBo.setRunRecord(runRecords);
        }

        if (CollectionUtils.isEmpty(ridingRecords)){
            userPortraitMovementInfoBo.setRidingRecord(null);
        } else {
            userPortraitMovementInfoBo.setRidingRecord(ridingRecords);
        }

        if (CollectionUtils.isEmpty(climbingRecords)){
            userPortraitMovementInfoBo.setClimbingRecord(null);
        } else {
            userPortraitMovementInfoBo.setClimbingRecord(climbingRecords);
        }

        if (CollectionUtils.isEmpty(swimRecords)){
            userPortraitMovementInfoBo.setSwimRecord(null);
        } else {
            userPortraitMovementInfoBo.setSwimRecord(swimRecords);
        }

        return userPortraitMovementInfoBo;
    }
}
